package com.abbas.abchat.dao.impl;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.abbas.abchat.dao.ChatTableDao;
import com.abbas.abchat.db.Sqlite;
import com.abbas.abchat.entity.ChatTable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/13 19:37
 */
public class ChatTableDaoImpl implements ChatTableDao {

    private SQLiteDatabase sqLiteDatabase;
    private String mTableName;

    public ChatTableDaoImpl(Context context,String tableName) {
        this.sqLiteDatabase = new Sqlite(context,Sqlite.DB_NAME,null,1).getWritableDatabase();
        this.mTableName = tableName;
    }

    @SuppressLint("Range")
    @Override
    public List<ChatTable> findAll() throws Exception {
        Cursor cursor = sqLiteDatabase.query(mTableName, null, null, null, null, null, null);
        List<ChatTable> list = new ArrayList<>();
        while (cursor.moveToNext()){
            ChatTable chatTable = new ChatTable();
            chatTable.setId(cursor.getInt(cursor.getColumnIndex(ChatTable.ID)));
            chatTable.setPeopleId(cursor.getInt(cursor.getColumnIndex(ChatTable.PEOPLE_ID)));
            chatTable.setChatName(cursor.getString(cursor.getColumnIndex(ChatTable.CHAT_NAME)));
            chatTable.setLastContent(cursor.getString(cursor.getColumnIndex(ChatTable.LAST_CONTENT)));
            chatTable.setLastTime(cursor.getString(cursor.getColumnIndex(ChatTable.LAST_TIME)));
            list.add(chatTable);
        }
        cursor.close();
        return list;
    }

    @Override
    public ChatTable findById(int _id) throws Exception {
        return null;
    }

    @Override
    public long insert(ChatTable entity) throws Exception {
        ContentValues contentValues = new ContentValues();
        contentValues.put(ChatTable.PEOPLE_ID,entity.getPeopleId());
        contentValues.put(ChatTable.CHAT_NAME,entity.getChatName());
        contentValues.put(ChatTable.LAST_CONTENT,entity.getLastContent());
        contentValues.put(ChatTable.LAST_TIME,entity.getLastTime());
        return sqLiteDatabase.insert(mTableName, null, contentValues);
    }

    @Override
    public int deleteById(int _id) throws Exception {
        return 0;
    }

    @Override
    public int updateById(ChatTable entity) throws Exception {
        return 0;
    }

    @Override
    public int updateByDbName(ChatTable entity) throws Exception {
        ContentValues contentValues = new ContentValues();
        contentValues.put(ChatTable.LAST_CONTENT,entity.getLastContent());
        contentValues.put(ChatTable.LAST_TIME,entity.getLastTime());
        return sqLiteDatabase.update(mTableName, contentValues, ChatTable.CHAT_NAME+" = ?", new String[]{entity.getChatName()});
    }

    @Override
    public int updateByPid(ChatTable entity) throws Exception {
        ContentValues contentValues = new ContentValues();
        contentValues.put(ChatTable.LAST_CONTENT,entity.getLastContent());
        contentValues.put(ChatTable.LAST_TIME,entity.getLastTime());
        return sqLiteDatabase.update(mTableName, contentValues, ChatTable.PEOPLE_ID+" = ?", new String[]{String.valueOf(entity.getPeopleId())});
    }

    @SuppressLint("Range")
    @Override
    public List<Integer> findAllOfId() throws Exception {
        List<Integer> ids = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query(mTableName, new String[]{ChatTable.PEOPLE_ID+""}, null, null, null, null, null);
        while (cursor.moveToNext()){
            ids.add(cursor.getInt(cursor.getColumnIndex(ChatTable.PEOPLE_ID)));
        }
        return ids;
    }
}
